package byggeTegning.husGeometri;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import javax.media.j3d.Group;
import javax.vecmath.Color3f;

import byggeTegning.geometry.Point3D;


// ***************************************************************************
// *** CLASS: SalTakSperrer
// ***************************************************************************
public class SalTakSperrer extends BjelkeLag
{
  protected double utspringLangSide=0.50; // 50 cm
  protected double utspringKortSide=0.45; // 45 cm
  protected double sperreHeight=0.25;     // 25 cm
  protected double vannBordHeight=0.10;   // 10 cm
    
  public SalTakSperrer
  ( String ident,
    Point3D p0,
    double width,
    double height,
    double depth,
    double utspringLangSide,
    double utspringKortSide,
    double sperreHeight,
    double vannBordHeight
  )
  { super(ident,p0,width,height,depth);
    this.utspringLangSide=utspringLangSide;
    this.utspringKortSide=utspringKortSide;
    this.sperreHeight=sperreHeight;
    this.vannBordHeight=vannBordHeight;
  }
  
  // ***************************************************************************
  // *** METHOD: getModel3D -- TEMP !!!
  // ***************************************************************************
  public /*abstract*/ Group getModel3D(Color3f c)
  { 
    return(null);
  }  

  public void drawProjection(int direction,Graphics2D g,Point2D.Double origo2D,double scale,Color c)
  { Rectangle2D.Double r=projection(direction,origo2D,scale);
    switch(direction)
    {
      case NORTH: drawVerticalCut2(g,r,scale,c); break;
      case SOUTH: drawVerticalCut2(g,r,scale,c); break;
      case EAST: drawVerticalCut(g,r,scale,c); break;
      case WEST: drawVerticalCut(g,r,scale,c); break;
//      case ABOVE: drawProjectionAbove(g,r,scale,c); break;
//      case BELOW: drawProjectionBelow(g,r,scale,c); break;
      case VCUT: drawVerticalCut(g,r,scale,c); break;
//      case HCUT: drawHorisontalCut(g,r,scale,c); break;
      default: super.drawProjection(direction,g,origo2D,scale,c);
    }  
  }  
    
  private void drawVerticalCut
  (Graphics2D g,Rectangle2D.Double r,double scale,Color c)
  { double sperreHeight=this.sperreHeight*scale;
    double vannBordHeight=this.vannBordHeight*scale;
    double dx=utspringLangSide*scale;
    double dy=dx*tan30;
    
//    r.y=r.y-6; // Ad'hoc korreksjon !!!
    r.y=r.y-(0.05*scale); // Ad'hoc korreksjon !!!
    
    GeneralPath path=new GeneralPath();
    float xt=(float)(r.x+r.width/2);
    float yt=(float)(r.y);
    path.moveTo(xt,(float)(yt-sperreHeight));
    path.lineTo((float)(r.x-dx),(float)(r.y+dy+r.height-sperreHeight));
    path.lineTo((float)(r.x-dx),(float)(r.y+dy+r.height-sperreHeight+vannBordHeight));
    path.lineTo((float)(r.x),(float)(r.y+r.height));
    path.lineTo(xt,yt);
    path.lineTo((float)(r.x+r.width),(float)(r.y+r.height));
    path.lineTo((float)(r.x+dx+r.width),(float)(r.y+dy+r.height-sperreHeight+vannBordHeight));
    path.lineTo((float)(r.x+dx+r.width),(float)(r.y+dy+r.height-sperreHeight));
    path.lineTo(xt,(float)(yt-sperreHeight));
//    fill(g,path,c);
    draw(g,path);
  }
    
  private void drawVerticalCut2
  (Graphics2D g,Rectangle2D.Double r,double scale,Color c)
  { double sperreHeight=this.sperreHeight*scale;
    double vannBordHeight=this.vannBordHeight*scale;
    double dx=utspringKortSide*scale;
    double dy=dx*tan30;

    double takStokkLength=utspringKortSide+r.width/scale+utspringKortSide;
      
    double dlx=60;
    int n=(int)((takStokkLength*scale+dlx)/dlx);
    
    double x1=r.x-dx; //takStokkUtspring*scale;
    double y1=r.y-sperreHeight;
    double w1=10;//r.width;
    double h1=r.height+sperreHeight+dy-(sperreHeight-vannBordHeight);
    dlx=(((takStokkLength*scale)-w1)/n); // Justert
    for(int i=0;i<n+1;i++)
    { Rectangle2D.Double r1=new Rectangle2D.Double(x1+(i*dlx),y1,w1,h1);
      fill(g,r1,c);
      draw(g,r1);
    }
    // Tegn taket
    double w2=takStokkLength*scale;
    double h2=r.height+sperreHeight+dy-(sperreHeight);
    Rectangle2D.Double rt=new Rectangle2D.Double(x1,y1,w2,h2);
    c=takFarge;
    fill(g,rt,c);
    draw(g,rt);
    // Tegn VannBord
    double w3=takStokkLength*scale;
    double y3=r.y+r.height+dy-(sperreHeight);
//    vannBordHeight=0.10*scale; // TEMP !!!!!
    Rectangle2D.Double rv=new Rectangle2D.Double(x1,y3,w3,vannBordHeight);
    c=rammeFarge;
    fill(g,rv,c);
    draw(g,rv);
  }
  
}